<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/3/20
 * Time: 22:06
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Controllers\Api\ApiController;
use App\Models\District;
use App\Models\DriverRoutes;
use Illuminate\Http\Request;
use Validator;

class DriverRoutesController extends ApiController
{
    /**
     * 司机订阅线路列表展示
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function routeList(Request $request)
    {
        try {

            $validator = Validator::make($request->all(), [
                'driver_uuid' => 'required|size:36',
                'page'        => 'required|integer',
                'limit'       => 'required|integer'
            ]);

            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $res = DriverRoutes::select(['*'])
                ->where('driver_uuid', $request->get('driver_uuid'))
                ->where('status', DriverRoutes::$statusOK) // 只展示启用状态下的
                ->orderBy('id', 'desc')
                ->paginate($request->get('limit', self::DEFAULT_PAGE_LIMIT))
                ->toArray();
            if (empty($res)) {
                return $this->success();
            }
            $oDistrict = new District();
            foreach ($res['data'] as $key => $value) {

                $container = [];
                array_push($container, $value['src_province_code']);
                array_push($container, $value['src_city_code']);
                array_push($container, $value['src_district_code']);

                array_push($container, $value['dst_province_code']);
                array_push($container, $value['dst_city_code']);
                array_push($container, $value['dst_district_code']);


                $result                                 = $oDistrict->getNameByAdcode($container);
                $res['data'][$key]['src_province_name'] = $result[$value['src_province_code']] ?? '';
                $res['data'][$key]['src_city_name']     = $result[$value['src_city_code']] ?? '';
                $res['data'][$key]['src_district_name'] = $result[$value['src_district_code']] ?? '';

                $res['data'][$key]['dst_province_name'] = $result[$value['dst_province_code']] ?? '';
                $res['data'][$key]['dst_city_name']     = $result[$value['dst_city_code']] ?? '';
                $res['data'][$key]['dst_district_name'] = $result[$value['dst_district_code']] ?? '';
            }

            return $this->table($res['data'], $res['total']);

        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 司机订阅线路 - 添加
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addRoute(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'driver_uuid'       => 'required|size:36',
                'src_province_code' => 'required',
                'src_city_code'     => 'required',
                'src_district_code' => 'required',
                'src_address'       => 'required',
                'dst_province_code' => 'required',
                'dst_city_code'     => 'required',
                'dst_district_code' => 'required',
                'dst_address'       => 'required',
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $data['driver_uuid']       = $request->get('driver_uuid');
            $data['src_province_code'] = $request->get('src_province_code');
            $data['src_city_code']     = $request->get('src_city_code');
            $data['src_district_code'] = $request->get('src_district_code');
            $data['src_address']       = $request->get('src_address');
            $data['dst_province_code'] = $request->get('dst_province_code');
            $data['dst_city_code']     = $request->get('dst_city_code');
            $data['dst_district_code'] = $request->get('dst_district_code');
            $data['dst_address']       = $request->get('dst_address');

            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');

            DriverRoutes::insert($data);

            return $this->success('success');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 编辑订阅线路
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editRoute(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'driver_uuid' => 'required|size:36',
                'route_id'    => 'required',

                'src_province_code' => 'required',
                'src_city_code'     => 'required',
                'src_district_code' => 'required',
                'src_address'       => 'required',
                'dst_province_code' => 'required',
                'dst_city_code'     => 'required',
                'dst_district_code' => 'required',
                'dst_address'       => 'required',
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $data['src_province_code'] = $request->get('src_province_code');
            $data['src_city_code']     = $request->get('src_city_code');
            $data['src_district_code'] = $request->get('src_district_code');
            $data['src_address']       = $request->get('src_address');
            $data['dst_province_code'] = $request->get('dst_province_code');
            $data['dst_city_code']     = $request->get('dst_city_code');
            $data['dst_district_code'] = $request->get('dst_district_code');
            $data['dst_address']       = $request->get('dst_address');

            $data['updated_at'] = date('Y-m-d H:i:s');

            DriverRoutes::where('id', $request->get('route_id'))
                ->where('driver_uuid', $request->get('driver_uuid'))
                ->update($data);

            return $this->success('success');

        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 司机订阅线路 - 删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteRoute(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'driver_uuid' => 'required|size:36',
                'route_id'    => 'required',
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $driverUuid = $request->get('driver_uuid');
            $routeId    = $request->get('route_id');

            DriverRoutes::where('driver_uuid', $driverUuid)
                ->where('id', $routeId)
                ->update(['status' => DriverRoutes::$statusNotOK]);

            return $this->success('success');

        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 订阅线路详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function routeDetail(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'driver_uuid' => 'required|size:36',
                'route_id'    => 'required',
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $driverUuid = $request->get('driver_uuid');
            $routeId    = $request->get('route_id');

            $oDriverRoute = DriverRoutes::where('driver_uuid', $driverUuid)
                ->where('id', $routeId)
                ->first();
            if (is_null($oDriverRoute)) {
                throw new \Exception('没有找到记录');
            }

            $data = $oDriverRoute->toArray();

            $container = [];
            array_push($container, $data['src_province_code']);
            array_push($container, $data['src_city_code']);
            array_push($container, $data['src_district_code']);

            array_push($container, $data['dst_province_code']);
            array_push($container, $data['dst_city_code']);
            array_push($container, $data['dst_district_code']);

            $result = (new District())->getNameByAdcode($container);

            $data['src_province_name'] = $result[$data['src_province_code']];
            $data['src_city_name']     = $result[$data['src_city_code']];
            $data['src_district_name'] = $result[$data['src_district_code']];

            $data['dst_province_name'] = $result[$data['dst_province_code']];
            $data['dst_city_name']     = $result[$data['dst_city_code']];
            $data['dst_district_name'] = $result[$data['dst_district_code']];

            return $this->success('success', $data);

        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


}